﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Zyg.Security
{
    public class SecurityHelper
    {
        public SecurityHelper()
        {
        }

        #region Fields

        byte[] iv = null;
        byte[] key = null;

        #endregion

        /// <summary>
        /// Init vector and key
        /// </summary>
        private void InitVectorAndKey(EncryptionAlgorithm algorithm)
        {
            if ((EncryptionAlgorithm.TripleDes == algorithm) ||
                   (EncryptionAlgorithm.Rijndael == algorithm))
            {
                // 3DES仅与16或24字节密钥一起使用
                key = Encoding.UTF8.GetBytes("password12345678");

                if (EncryptionAlgorithm.Rijndael == algorithm)
                {
                    // 对于 Rijndael, initial vector 必须为16字节
                    iv = Encoding.UTF8.GetBytes("init vec is big.");
                }
                else
                {
                    // DES,3DES,RC2为8字节
                    iv = Encoding.UTF8.GetBytes("zygsecur");
                }
            }
            else
            {
                // DES 仅使用8字节密钥
                key = Encoding.UTF8.GetBytes("ioewmnf;");
                iv = Encoding.UTF8.GetBytes("init vec");
            }
        }

        /// <summary>
        /// Encrypt
        /// </summary>
        public byte[] Encrypt(byte[] plainText, EncryptionAlgorithm algorithm)
        {
            InitVectorAndKey(algorithm);

            Encryptor enc = new Encryptor(algorithm);

            enc.IV = iv;

            // 加密
            byte[] ciperText = enc.Encrypt(plainText, key);

            return ciperText;
        }

        /// <summary>
        /// Encrypt
        /// </summary>
        public string Encrypt(string plainText, EncryptionAlgorithm algorithm)
        {
            byte[] ciperText = Encrypt(Encoding.UTF8.GetBytes(plainText), algorithm);

            return Convert.ToBase64String(ciperText);
        }

        /// <summary>
        /// Decrypt
        /// </summary>
        public byte[] Decrypt(byte[] ciperText, EncryptionAlgorithm algorithm)
        {
            InitVectorAndKey(algorithm);

            Decryptor dec = new Decryptor(algorithm);

            dec.IV = iv;

            byte[] plainText = dec.Decrypt(ciperText, key);

            return plainText;
        }

        /// <summary>
        /// Decrypt
        /// </summary>
        public string Decrypt(string ciperText, EncryptionAlgorithm algorithm)
        {
            byte[] plainText = Decrypt(Convert.FromBase64String(ciperText), algorithm);

            return Encoding.UTF8.GetString(plainText);
        }

        /// <summary>
        /// MD5 Hash
        /// </summary>
        public string ComputeMD5Hash(string data)
        {
            Hasher hasher = new Hasher();

            return hasher.ComputeMD5Hash(data);
        }
		
		/// <summary>
        /// MD5 Hash
        /// </summary>
        public string ComputeMD5Hash(string data, Encoding encoding)
        {
            Hasher hasher = new Hasher();

            return hasher.ComputeMD5Hash(data, encoding);
        }

        /// <summary>
        /// SHA-1 Hash
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ComputeSHA1Hash(string data)
        {
            Hasher hasher = new Hasher();

            return hasher.ComputeSHA1Hash(data);
        }
		
		/// <summary>
        /// SHA-1 Hash
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string ComputeSHA1Hash(string data, Encoding encoding)
        {
            Hasher hasher = new Hasher();

            return hasher.ComputeSHA1Hash(data, encoding);
        }
    }
}
